package com.organization.services

import java.rmi.RemoteException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import com.organization.model.Department
import com.organization.model.Project

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 09.07.12
 * Time: 9:56
 * To change this template use File | Settings | File Templates.
 */
@Service
class TreeService {

    @Autowired
    def commonService
    @Autowired
    def departmentService
    @Autowired
    def projectService
    @Autowired
    def employeeService

    //get branch for root node
    protected def getTree() {
        return departmentService.departmentList().collect{[
                text : it.toString(),
                id   : (commonService.depNodeStr+it.id),
                children: getProjectsTreeByDep(it.id)
        ]}
    }

    //get brunch for department node
    protected def getProjectsTreeByDep(def depId){
        Department dep = departmentService.get(depId)
        return projectService.findAllByDepartment(dep).collect{[
                text : it.toString(),
                id   : (commonService.projNodeStr+it.id),
                children : getEmployeeTreeByProj(it.id)
        ]}
    }

    //get brunch for project node
    protected def getEmployeeTreeByProj(def projId){
        Project proj = projectService.get(projId)
        return employeeService.findAllByProject(proj).collect{[
                text : it.toString(),
                id   : (commonService.emplNodeStr+it.id),
                leaf : "true"
        ]}
    }
}
